import os
import shutil
import sys
def list_files_excluding_folder(startpath, exclude_folder_name,destination_folder_path):
    for root, dirs, files in os.walk(startpath):
        if exclude_folder_name in dirs:
            dirs.remove(exclude_folder_name)  # 排除特定文件夹
        if ".gradle" in dirs:
            dirs.remove(".gradle")  # 排除特定文件夹
        if ".idea" in dirs:
            dirs.remove(".idea")  # 排除特定文件夹
        if ".git" in dirs:
            dirs.remove(".git")  # 排除特定文件夹
        for file in files:
            temp_path = destination_folder_path+os.path.join(root, file)[len(startpath):-len(file)-1]
            if not os.path.exists(temp_path):
                print("创建文件 "+temp_path)
                os.makedirs(destination_folder_path+os.path.join(root, file)[len(startpath):-len(file)-1])
            shutil.copy(os.path.join(root, file),(destination_folder_path+os.path.join(root, file)[len(startpath):-len(file)-1]))
        


def delete_files_in_folder(folder_path):
    for file_name in os.listdir(folder_path):
        file_path = os.path.join(folder_path, file_name)
        try:
            if os.path.isfile(file_path):
                os.remove(file_path)
                print(f"Deleted file: {file_path}")
        except Exception as e:
            print(f"Error deleting file {file_path}: {str(e)}")

# gpus = sys.argv[1]
# print(sys.argv)
# print(gpus)


# 使用命令 python.exe copyFiles.py D:\svn\MT8786_H1641RP_Android14\hikvision\packages\FactoryTest\FactoryTest
# 将.py文件 根目录的全部文件复制到指定目录 注意1.全部文件除 build .gradle .idea文件夹除外  2.指定目录的原文件会被全部删除
folder_path = os.path.dirname(os.path.abspath(__file__))
exclude_folder_name = 'build'
# to_path = sys.argv[1]
to_path = "C:\\Users\\zhangdi45\\Desktop\\ad13RFID对外资料\\RFIDDemo"
if "D:\\project" in to_path :
    print("source 和 destination 路径混乱")
    sys.exit(0)
print("删除旧文件...")
delete_files_in_folder(to_path)
print("删除成功，开始复制文件...")
list_files_excluding_folder(folder_path, exclude_folder_name,to_path)
print("复制文件成功")